package com.leetcode.offer.chapter4;

import com.leetcode.basic.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dennis Li
 * @date 2020/7/13 20:54
 */
public class LevelOrder2_32 {
    List<List<Integer>> ans;
    // 每一行打印
    public List<List<Integer>> levelOrder(TreeNode root) {
        ans = new ArrayList<>();
        dfs(root, 0);
        return ans;
    }
    private void dfs(TreeNode root, int layer) {
        if (root == null) return;
        if (ans.size() < layer) ans.add(new ArrayList<>());
        ans.get(layer).add(root.val);
        dfs(root.left, layer + 1);
        dfs(root.right, layer + 1);
    }
}
